package com.meirimoney.app;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.meirimoney.app.control.MenuBar;
import com.meirimoney.sdk.models.Commodity;
import com.meirimoney.task.ExchangeTask;

public class BaseExchange extends BaseAccount implements OnClickListener
{
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		if (null == this.commodity)
		{
			Intent i = this.getIntent();
			if (null != i)
			{
				Bundle bd = i.getBundleExtra("commodity");
				if (null != bd)
				{
					this.commodity = this.getCommodity(bd);
					if (this.commodity.Category == 1)
					{
						this.caption = "兑换手机话费";
					} else if (this.commodity.Category == 2)
					{
						this.caption = "提现到支付宝";
					}
				}
			}
		}
	}

	/**
	 * 检测兑换余额
	 * 
	 * @return
	 */
	public boolean checkBalance()
	{
		if (this.getUser().getIntegralBalance() < this.commodity.Integral)
		{
			this.settingDialog("余额不足", "您当前的余额不足，再去做些任务吧，马上就能兑换了！", "返回",
					"立即赚取", new DialogInterface.OnClickListener()
					{
						public void onClick(DialogInterface dialog, int which)
						{
							if (DialogInterface.BUTTON_POSITIVE == which)
							{
								BaseExchange.this.startActivity(new Intent(
										BaseExchange.this,
										AdvertisingPanel.class));

							} else if (DialogInterface.BUTTON_NEGATIVE == which)
							{
								dialog.dismiss();
								BaseExchange.this.finish();
							}
						}
					});
			return false;
		}
		return true;
	}

	/**
	 * 根据传入的Bundle对象获取Commodity对象
	 * 
	 * @param bd
	 * @return
	 */
	private Commodity getCommodity(Bundle bd)
	{
		Commodity commodity = new Commodity();
		commodity.Category = bd.getInt("category");
		commodity.Integral = bd.getInt("integral");
		commodity.Price = bd.getDouble("price");
		commodity.Id = bd.getLong("id");
		return commodity;
	}

	/***
	 * 重定向至成功页面
	 */
	public void redirect()
	{
		this.setContentView(R.layout.mrm_complete);
		this.navigation = (MenuBar) this.findViewById(R.id.head);
		this.setTitle(this.caption);
		
		Button earning=(Button)this.findViewById(R.id.earning);
		earning.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				BaseExchange.this.startActivity(new Intent(
						BaseExchange.this,
						AdvertisingPanel.class));
			}
		});
	}

	@Override
	public void onClick(View v)
	{
		this.submitButton.setOnClickListener(null);
		this.submitButton.setText("提交中……");
	}

	/**
	 * 提交兑换申请
	 */
	public void submit(int count, String shipAddress)
	{
		if (this.task != null)
		{
			AsyncTask.Status status = this.task.getStatus();
			if (status != AsyncTask.Status.FINISHED)
			{
				return;
			}
		}
		this.task = new ExchangeTask(this);
		this.task.execute(this.commodity.Id, count, shipAddress, this.getUser()
				.getToken());
	}

	/**
	 * 设置标题
	 */
	protected void setTitle()
	{
		if (null == this.navigation)
		{
			this.navigation = (MenuBar) this.findViewById(R.id.head);
		}
		this.setTitle(this.caption);
	}

	private ExchangeTask task;

	/**
	 * 商品对象
	 */
	protected Commodity commodity;

	/**
	 * 标题提示语
	 */
	protected TextView hintTab;

	/**
	 * 标题
	 */
	protected String caption;

	/**
	 * 提交按钮
	 */
	protected Button submitButton;
}
